package cn.com.shopec.erp.purchase.commo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.com.shopec.erp.base.service.IAuditSettingService;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.RandomUtil;
import cn.com.shopec.erp.common.utils.SpringUtil;
import cn.com.shopec.erp.purchase.entity.PoProduct;
import cn.com.shopec.erp.purchase.entity.PurchaseOrder;
import cn.com.shopec.erp.purchase.model.PoAuditPoint;
import cn.com.shopec.erp.purchase.model.PoProductInfo;
import cn.com.shopec.erp.purchase.model.PurchaseOrderDetail;
import cn.com.shopec.erp.purchase.model.PurchaseOrderInfo;
import cn.com.shopec.erp.purchase.model.PurchaseOrderQuery;
import cn.com.shopec.erp.purchase.vo.PurchaseOrderQueryVo;
import cn.com.shopec.erp.system.constant.SystemConstant;

/** 
 * @ClassName: POPropertyUtils 
 * @Description: 采购单相关对象转换工具类
 * @author: lule
 * @date 2013-5-22 下午4:42:14
 *  
 */

public class POPropertyUtils {
	
	//用
	/** 
	 * @Title: copyPurchaseOrder2PurchaseOrderInfo 
	 * @Description: 拷贝PurchaseOrder实体的属性到PurchaseOrderInfo
	 * @param po
	 * @param o  
	 * @throws 
	 */
	public static void copyPurchaseOrder2PurchaseOrderInfo(PurchaseOrder po, PurchaseOrderInfo o) {
		if(po != null && o != null) {
			o.setPoNo(po.getPoNo());
//			o.setPrNo(po.getPrNo());
			o.setId(po.getId());
			o.setPurcType(po.getPurcType());
			o.setFoPurDate(po.getFoPurDate());
			o.setAcPurDate(po.getAcPurDate());
			o.setPoStatus(po.getPoStatus());
			o.setCurAuditPoint(po.getCurAuditPoint());
			o.setLastAuditRole(po.getLastAuditRole());
//			o.setCreatePerson(po.getCreateUser() != null ? po.getCreateUser().getUserName() : null);
//			o.setCreatePersonName(po.getCreateUser() != null ? po.getCreateUser().getRealName() : null);
			o.setCreatePerson(po.getCreatePerson() !=null ? po.getCreatePerson() : null);
			o.setCreateTime(po.getCreateTime());			
			o.setOverData(po.getOverData());
			
			List<PoProduct> poProds = po.getPoProducts();
			if(poProds != null) {
				int totalQuantity = 0;
				double totalAmount = 0.0d;
				for(PoProduct poProd : poProds) {
					totalQuantity += poProd.getPurQuantity();
					totalAmount += poProd.getPurUnitPrice() * poProd.getPurQuantity();
				}
				
				o.setTotalQuantity(totalQuantity);
				o.setTotalAmount(totalAmount);
			}
			
			/*List<PoAuditHistory> poAuHitories = po.getPoAuditHistories();
			if(poAuHitories != null && poAuHitories.size() != 0) {
				if(poAuHitories.size() > 1) {
					Collections.sort(poAuHitories, new Comparator<PoAuditHistory>() {
						public int compare(PoAuditHistory o1, PoAuditHistory o2) {
							return -(o1.getAuditTime().compareTo(o2.getAuditTime()));
						}
						
					});
				}
				o.setLastAuditPerson(poAuHitories.get(0).getAuditPerson());
				o.setLastAuditTime(poAuHitories.get(0).getAuditTime());
				o.setLastAuditPersonName(poAuHitories.get(0).getAuditPersonUser() != null ? poAuHitories.get(0).getAuditPersonUser().getRealName() : null);
			}*/
			o.setLastAuditPerson(po.getLastAuditPerson());
			o.setLastAuditTime(po.getLastAuditTime());
		}
	}

	/** 
	 * @Title: getPurchaseOrderInfo 
	 * @Description: 采购单实体对象转采购单基本信息model类
	 * @param po
	 * @return  
	 * @throws 
	 */
	public static PurchaseOrderInfo getPurchaseOrderInfo(PurchaseOrder po) {
		PurchaseOrderInfo o = null;
		if(po != null) {
			o = new PurchaseOrderInfo();
			copyPurchaseOrder2PurchaseOrderInfo(po, o);
			if (SpringUtil.getBean(IAuditSettingService.class).checkNeedsAudit(SystemConstant.AUDIT_SETTING_2, String.valueOf(o.getTotalAmount()))) {
				o.setAchievePDOrOSAudit(true);
			}
			if (SpringUtil.getBean(IAuditSettingService.class).checkNeedsAudit(SystemConstant.AUDIT_SETTING_3, String.valueOf(o.getTotalAmount()))) {
				o.setAchieveFPAudit(true);
			}
			if (SpringUtil.getBean(IAuditSettingService.class).checkNeedsAudit(SystemConstant.AUDIT_SETTING_4, String.valueOf(o.getTotalAmount()))) {
				o.setAchieveGMAudit(true);
			}
		}
		return o;
	}
	
	//用
	/** 
	 * @Title: getPurchaseOrderDetail 
	 * @Description: 将PurchaseOrder实体对象转为PurchaseOrderDetail对象
	 * @param po
	 * @return  
	 * @throws 
	 */
	public static PurchaseOrderDetail getPurchaseOrderDetail(PurchaseOrder po) {
		PurchaseOrderDetail o = null;
		if(po != null) {
			o = new PurchaseOrderDetail();
			copyPurchaseOrder2PurchaseOrderInfo(po, o);
			
			o.setPoProducts(getPoProductInfoList(po.getPoProducts()));
//			o.setPoAuditHistories(getPoAuditHistoryInfoList(po.getPoAuditHistories()));
		}
		return o;
	}
	
	/** 
	 * @Title: getPurchaseInfoList 
	 * @Description: 采购单实体对象的列表转采购单基本信息model类对象的列表
	 * @param poList
	 * @return  
	 * @throws 
	 */
	/*public static List<PurchaseOrderInfo> getPurchaseInfoList(List<PurchaseOrder> pList) {
		List<PurchaseOrderInfo> oList = new ArrayList<PurchaseOrderInfo>(pList == null ? 0 : pList.size());
		if(pList != null) {
			for(PurchaseOrder po : pList) {
				Hibernate.initialize(po.getPoAuditHistories());
				oList.add(getPurchaseOrderInfo(po));
			}
		}
		
		return oList;
	}*/
	
	/** 
	 * @Title: getPurchaseOrderQuery 
	 * @Description: 将PurchaseOrderQueryVo对象转为PurchaseOrderQuery对象
	 * @param qVo
	 * @return  
	 * @throws 
	 */
	public static PurchaseOrderQuery getPurchaseOrderQuery(PurchaseOrderQueryVo qVo) {
		PurchaseOrderQuery o = null;
		if(qVo != null) {
			o = new PurchaseOrderQuery();
			o.setPoNo(qVo.getPoNo());
//			o.setPrNo(qVo.getPrNo());
			o.setPoStatus(qVo.getPoStatus());
			o.setCreatePersonName(qVo.getCreatePersonName());
			o.setPurDateStart(qVo.getPurDateBegin());
			o.setPurDateEnd(qVo.getPurDateEnd());
			o.setCreatePerson(qVo.getCreatePerson());
			o.setAuPoint(PoAuditPoint.getPoAuditPointByCode(qVo.getCurAuditPoint()));
			o.setDispatchingStatus(qVo.getDispatchingStatus());
		}
		
		return o;
	}
	
	/** 
	 * @Title: getPurchaseOrderFormPurchaseReqVo 
	 * @Description: 请购单vo转采购单实体
	 * @param prVo
	 * @return  
	 * @throws 
	 */
	/*public static PurchaseOrder getPurchaseOrderFormPurchaseReqVo(PurchaseRequisitionVo prVo) {
		PurchaseOrder po = new PurchaseOrder();
		po.setPoNo(genPurchaseOrderNo());
		po.setPrNo(prVo.getPrNo());
		po.setFoPurDate(prVo.getPrEstimatedDate() == null ? null : prVo.getPrEstimatedDate().substring(0, DateUtil.DEFAULT_PATTERN.length()));
		po.setPoStatus(SZSCMConstant.PO_STATUS_APPLYING);
		po.setCurAuditPoint(PoAuditPoint.PO_AUDIT_POINT_PURCH_SU.getCode());
		po.setCreateTime(DateUtil.formatDateByFormat(new Date(), DateUtil.TIMESTAMP_PATTERN));
		po.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
		po.setCreatePerson(prVo.getPrUser());
		po.setPurcType(prVo.getPrType());
		po.setOverData(prVo.getPrArriveDate());
		return po;
	}*/
	
	/** 
	 * @Title: getPoProductsFromFurchaseReqVo 
	 * @Description: 从请购单vo得到采购单商品实体的列表
	 * @param prVo
	 * @param poNo
	 * @return  
	 * @throws 
	 */
	/*public static List<PoProduct> getPoProductsFromFurchaseReqVo(PurchaseRequisitionVo prVo, String poNo) {
		List<PurchaseRequisitionProductVo> prProdList = prVo.getPrpvlist();
		List<PoProduct> poProdList = new ArrayList<PoProduct>(prProdList != null ? prProdList.size() : 0);
		if(prProdList != null) {
			for(PurchaseRequisitionProductVo prProd : prProdList) {
				PoProduct poProd = new PoProduct();
				
				poProd.setPoNo(poNo);
				poProd.setProdNo(prProd.getPpGoodsNo());
				poProd.setCommodityName(prProd.getPpName());
				poProd.setPurQuantity(prProd.getPpNumber());
				poProd.setSpecification(prProd.getPpSpecification());
				poProd.setPurSuppNo(prProd.getPpSupplierNo());
				poProd.setPurSuppName(prProd.getPpSupplierName());
				poProd.setPurUnitPrice(Double.parseDouble(prProd.getPpUnitPrice()));
				poProd.setUnit(prProd.getPpUnit());
				poProd.setPurWhNo(prProd.getPpWarehouseNo());
				poProd.setPurWhName(prProd.getPpWarehouseName());
				poProd.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
				
				poProdList.add(poProd);
			}
		}
		return poProdList;
	}*/
	
	/** 
	 * @Title: getPoProductInfo 
	 * @Description: 采购单商品实体对象转采购单商品信息model对象
	 * @param p
	 * @return  
	 * @throws 
	 */
	public static PoProductInfo getPoProductInfo(PoProduct p) {
		if(p == null)
			return null;
		
		PoProductInfo o = new PoProductInfo();
		o.setPoNo(p.getPoNo());
		o.setProdNo(p.getProdNo());
		o.setCommodityName(p.getCommodityName());
		o.setSpecification(p.getSpecification());
		o.setUnit(p.getUnit());
		o.setPurUnitPrice(p.getPurUnitPrice());
		o.setPurQuantity(p.getPurQuantity());
		o.setPurSuppNo(p.getPurSuppNo());
		o.setPurSuppName(p.getPurSuppName());
		o.setPurWhNo(p.getPurWhNo());
		o.setPurWhName(p.getPurWhName());
		return o;
	}

	//用
	/** 
	 * @Title: getPoProductInfoList 
	 * @Description: 将采购单商品实体对象的列表，转为采购单商品信息对象的列表
	 * @param pList
	 * @return  
	 * @throws 
	 */
	public static List<PoProductInfo> getPoProductInfoList(List<PoProduct> pList) {
		List<PoProductInfo> oList = new ArrayList<PoProductInfo>(pList == null ? 0 : pList.size());
		if(pList != null) {
			for(PoProduct p : pList) {
				oList.add(getPoProductInfo(p));
			}
		}

		return oList;
	}
	
	/** 
	 * @Title: getPoAuditHistoryInfo 
	 * @Description: 采购单审核历史实体对象，转为采购单审核历史信息model类对象
	 * @param p
	 * @return  
	 * @throws 
	 */
/*	public static PoAuditHistoryInfo getPoAuditHistoryInfo(PoAuditHistory p) {
		if(p == null)
			return null;
		
		PoAuditHistoryInfo o = new PoAuditHistoryInfo();
		o.setPoNo(p.getPoNo());
		o.setAuditTime(p.getAuditTime());
		o.setAuditResult(p.getAuditResult());
		o.setAuditPerson(p.getAuditPersonUser() == null ? null : p.getAuditPersonUser().getUserName());
		o.setAuditPersonName(p.getAuditPersonUser() == null ? null : p.getAuditPersonUser().getRealName());
		o.setRemark(p.getRemark());
		
		return o;
	}*/
	
	/** 
	 * @Title: getPoAuditHistoryInfoList 
	 * @Description: 采购单审核历史实体对象的列表，转为采购单审核历史信息model类对象的列表
	 * @param pList
	 * @return  
	 * @throws 
	 */
	/*public static List<PoAuditHistoryInfo> getPoAuditHistoryInfoList(List<PoAuditHistory> pList) {
		List<PoAuditHistoryInfo> oList = new ArrayList<PoAuditHistoryInfo>(pList == null ? 0 : pList.size());
		if(pList != null) {
			for(PoAuditHistory p : pList) {
				oList.add(getPoAuditHistoryInfo(p));
			}
		}
		
		return oList;
	}*/

	/** 
	 * @Title: getPoAuditHistoryFromPoAudit 
	 * @Description: 从请购单申请数据中，生成请购单审核历史的实体
	 * @param poAudit
	 * @return  
	 * @throws 
	 */
	/*public static PoAuditHistory getPoAuditHistoryFromPoAudit(PurchaseOrderAudit poAudit) {
		if(poAudit != null) {
			PoAuditHistory his = new PoAuditHistory();
			his.setPoNo(poAudit.getPoNo());
			his.setAuditPerson(poAudit.getAuditPerson());
			his.setAuditResult(poAudit.getAuditResult());
			his.setAuditTime(DateUtil.formatDateByFormat(new Date(), DateUtil.TIMESTAMP_PATTERN));
			his.setRemark(poAudit.getRemark());
			his.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
			
			return his;
		} else {
			return null;
		}
	}*/
	
	/** 
	 * @Title: getPoAuditPoint 
	 * @Description: 根据PO审核人身份，得到他们对应的审核节点
	 * @param auditor
	 * @return  
	 * @throws 
	 */
	/*public static PoAuditPoint getPoAuditPoint(PoAuditor auditor) {
		PoAuditPoint p = null;
		if(PoAuditor.PO_AUDITOR_PURCH_SU == auditor)
			p = PoAuditPoint.PO_AUDIT_POINT_PURCH_SU;
		else if(PoAuditor.PO_AUDITOR_PURCH_DR == auditor || PoAuditor.PO_AUDITOR_OPERA_SU == auditor)
			p = PoAuditPoint.PO_AUDIT_POINT_PURCH_DR_OPERA_SU;
		else if(PoAuditor.PO_AUDITOR_FIN_PR == auditor)
			p = PoAuditPoint.PO_AUDIT_POINT_FIN_PR;
		else if(PoAuditor.PO_AUDITOR_GEN_MNG == auditor)
			p = PoAuditPoint.PO_AUDIT_POINT_GEN_MNG;
		
		return p;
	}*/
	
	/**
	 * 生成采购单号 
	 * @return  
	 * @throws 
	 */
	public static String genPurchaseOrderNo() {
		//TODO 
		String no = "PO" + DateUtil.formatDateByFormat(new Date(), DateUtil.NOCHAR_PATTERN) + RandomUtil.getRandomNumberStr(5);
		
		return no;
	}
	
	
	/**
         * 生成退购单单号 
         * @return  
         * @throws 
         */
        public static String genReturnOrderNo() {
                //TODO 
                String no = "PT" + DateUtil.formatDateByFormat(new Date(), DateUtil.NOCHAR_PATTERN) + RandomUtil.getRandomNumberStr(5);
                
                return no;
        }
	
	
	/**
	 * 生成购销结算单号
	 * 
	 * */
	
	public static String genBuyingOrderNo() {
             //TODO 
             String no = "PS" + DateUtil.formatDateByFormat(new Date(), DateUtil.NOCHAR_PATTERN) + RandomUtil.getRandomNumberStr(5);
             
             return no;
     }
	
}
